<template>
  <view class="gushi-page">
    <u-navbar placeholder title="故事" title-style="color: #fff;" bg-color="transparent" left-icon-color="#fff" auto-back />
    <image class="gushi-bg" src="https://cbandu.oss-cn-qingdao.aliyuncs.com/imgs/20250828/bg_gushi.png" />

    <view class="gushi-content">
      <view class="content-tabber">
        <view v-for="item in tabberList" :key="item.categoryId" class="tabber-item" :class="{ active: currentTab === item.categoryId }" @click="setTab(item.categoryId)">
          {{ item.name }}
        </view>
      </view>

      <scroll-view scroll-y="auto" class="content-list" @scrolltolower="loadMore">
        <view v-for="item in microclassList" :key="item.id" class="list-item" @click="toDetail(item)">
          <image class="item-img" :src="`https://shuyutk.oss-cn-qingdao.aliyuncs.com${item.img}`" mode="aspectFill" />

          <view class="item-content">
            <view class="content-title">
              {{ item.title }}
            </view>
            <view class="content-info">
              <view v-if="item.vipFlag === '1'" class="info-vip">
                VIP
              </view>
              <view class="info-type" :class="[item.courseType % 2 ? 'type-video' : 'type-audio']">
                {{ item.courseType % 2 ? '视频' : '音频' }}
              </view>
              <!-- <view class="info-time">
                <image class="time-icon" src="https://cbandu.oss-cn-qingdao.aliyuncs.com/imgs/20250828/time.png" />
                <text class="time-text">
                  {{ item.videoDuration }}
                </text>
              </view> -->
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <ContactTeacher v-model:show-overlay="showTeacher" :type="3" />
  <LoginOverlay v-model:show="showLogin" />
</template>

<script lang="ts" setup>
import { isLogin } from '@/utils/auth';
import { useUserStore } from '@/store';
import type { ListPageParams } from '@/api/read/type';
import { getCategory, getMicroclassList } from '@/api/microClassroom';
import type { GetMicroclassListParams, GetMicroclassListResult, getCategoryResult } from '@/api/microClassroom/type';

const store = useUserStore();
const loginFlag = ref<boolean>(isLogin());
const showLogin = ref<boolean>(false);
const showTeacher = ref<boolean>(false);
const hasSchool = computed(() => !!store.schoolId); // 是否绑定门店

const currentTab = ref(0);
const tabberList = ref<{ categoryId: number; name: string }[]>([]);
const pageParams = ref<ListPageParams>({
  pageNum: 1,
  pageSize: 20,
});
const microclassList = ref<Array<GetMicroclassListResult>>([]);
const total = ref(0);

const toDetail = (item: GetMicroclassListResult) => {
  if (item.vipFlag === '1') { // vip数据
    if (!loginFlag.value) { // 未登录
      showLogin.value = true;
      return;
    }
    if (!hasSchool.value) { // 未绑定门店
      showTeacher.value = true;
      return;
    }
    if ([0, 3].includes(store.vip.flag)) { // 非vip
      uni.$u.toast(store.vip.flag === 0 ? 'VIP未开通！' : 'VIP未生效！');
      return;
    }
    if (store.vip.flag === 2) { // vip过期
      uni.$u.toast('VIP已过期！');
      return;
    }
  }

  uni.navigateTo({
    url: `/pages/views/microClassroom/detail?id=${item.id}&vipFlag=${item.vipFlag}`,
  });
};

const getList = () => {
  const params: GetMicroclassListParams = {
    oneCategoryId: '1',
    ...pageParams.value,
  };
  if (currentTab.value) {
    params.twoCategoryId = currentTab.value;
  }
  return getMicroclassList(params).then((res: { total: number; rows: Array<GetMicroclassListResult> }) => {
    console.log(res);
    microclassList.value = microclassList.value.concat(res.rows);
    total.value = res.total;
  });
};

const setTab = (id: number) => {
  currentTab.value = id;
  microclassList.value = [];
  pageParams.value.pageNum = 1;
  getList();
};

const loadMore = () => {
  if (microclassList.value.length >= total.value)
    return;
  pageParams.value.pageNum++;
  getList();
};

const getData = () => {
  getCategory({
    categoryId: '1',
  }).then((res: getCategoryResult[]) => {
    tabberList.value = [
      {
        categoryId: 0,
        name: '全部',
      },
      ...res[0]?.children,
    ];
    getList();
  });
};

getData();
</script>

<style lang="scss" scoped>
.gushi-page {
  position: relative;
  width: 100%;
  height: 100%;
  padding-top: 100rpx;
  overflow: hidden;

  .gushi-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 750rpx;
    height: 280rpx;
    object-fit: cover;
  }

  .gushi-content {
    position: relative;
    padding: 46rpx 0;
    height: 100%;
    background: #FFFFFF;
    border-radius: 35rpx 35rpx 0 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    .content-tabber {
      width: 100%;
      padding: 0 24rpx;
      display: flex;
      overflow: auto;

      .tabber-item {
        flex-shrink: 0;
        padding: 0 24rpx;
        margin: 0 10rpx;
        border-radius: 32rpx;
        background: #F5F5F5;
        font-size: 29rpx;
        color: #999;
        line-height: 64rpx;
      }

      .tabber-item.active {
        background: #fff;
        border: 2rpx solid #31C966;
        color: #31C966;
        line-height: 60rpx;
      }

      &::before {
        content: '';
        position: absolute;
        top: 46rpx;
        left: 0;
        width: 40rpx;
        height: 63rpx;
        background: linear-gradient( 90deg, #fff 0%, #fff 39%, rgba(255,255,255,0) 100%);
        z-index: 2;
      }

      &::after {
        content: '';
        position: absolute;
        top: 46rpx;
        right: 0;
        width: 40rpx;
        height: 63rpx;
        background: linear-gradient( 270deg, #fff 0%, #fff 39%, rgba(255,255,255,0) 100%);
      }
    }

    .content-list {
      flex: 1;
      overflow: auto;
      padding: 0 30rpx 68rpx;
      margin-top: 38rpx;
      box-sizing: border-box;

      .list-item {
        padding-bottom: 44rpx;
        display: flex;

        .item-img {
          width: 148rpx;
          height: 148rpx;
          border-radius: 25rpx;
        }

        .item-content {
          flex: 1;
          padding-left: 25rpx;
          display: flex;
          flex-direction: column;

          .content-title {
            padding-top: 12rpx;
            font-size: 35rpx;
            color: #3D3D3D;
            line-height: 50rpx;
          }

          .content-info {
            padding-top: 17rpx;
            display: flex;
            align-items: center;

            .info-vip {
              padding: 0 8rpx;
              margin-right: 12rpx;
              font-size: 21rpx;
              color: #fff;
              line-height: 32rpx;
              background: #FFC329;
              border-radius: 6rpx;
            }

            .info-type {
              padding: 0 8rpx;
              margin-right: 27rpx;
              font-size: 21rpx;
              line-height: 32rpx;
              border-radius: 6rpx;
            }

            .type-video {
              background: #FFEED1;
              color: #FF9532;
            }

            .type-audio {
              background: #CDD9FF;
              color: #567EFF;
            }

            .info-time {
              display: flex;
              align-items: center;

              .time-icon {
                width: 25rpx;
                height: 25rpx;
                margin-right: 8rpx;
              }

              .time-text {
                font-size: 21rpx;
                color: #B3B3B3;
                line-height: 31rpx;
              }
            }
          }
        }
      }
    }
  }
}
</style>
